<template>
	<view>
		<!-- <web-view src="https://mobile-dev.yilife.cn/identificationSuccess?result=true" /> -->
		<web-view :src="faceUrl"
			><!-- <cover-view class="bottom-button-bd"><button class="primary-button w90">返回上一页</button></cover-view> --></web-view
		>
	</view>
</template>
<script>
export default {
	data() {
		return {
			faceUrl: '',
			classId: '',
			pagePath: '/pages/study/index',
			type: 0,
			source: 1,
			instanceId: '',
			nodeId: '',
		}
	},
	onShow() {
		// this.querySessionToken()
	},
	onLoad(options) {
		this.instanceId = options.instanceId
		this.nodeId = options.nodeId
		this.getUserVerification()
	},
	methods: {
		//判断是否已有源
		getUserVerification() {
			this.$request
				.request(
					'learn-platform/userVerification/getUserVerification',
					{
						method: 'post',
						data: {
							phones: uni.getStorageSync('userPhone'),
						},
					}
				)
				.then((res) => {
					if (res.success) {
						if (res.data.length > 0) {
							this.type = 2
							this.source = 0
						}
						this.querySessionToken()
					}
				})
		},
		querySessionToken() {
			this.$request
				.request('learn-platform/volc/face/querySessionToken', {
					method: 'get',
					data: {},
				})
				.then((res) => {
					if (res.success) {
						const { accessKeyId, secretAccessKey, sessionToken } =
							res.data
						this.uploadReferencePicture(
							accessKeyId,
							secretAccessKey,
							sessionToken
						)
					}
				})
		},
		uploadReferencePicture(accessKeyId, secretAccessKey, sessionToken) {
			this.$request
				.request('learn-platform/volc/face/uploadReferencePicture', {
					method: 'post',
					data: {
						accessKeyId,
						secretAccessKey,
						sessionToken,
						refSource: this.source,
					},
				})
				.then((res) => {
					if (res.success) {
						const { type, source } = this
						let baseUrl = ''
						if (
							this.$test.$test ===
							'https://gateway-dev.yilife.cn/'
						) {
							baseUrl = 'https://mobile-dev.yilife.cn/faceAuth'
						} else {
							baseUrl = 'https://mobile.yilife.cn/faceAuth'
						}
						const redirectUrl = encodeURIComponent(
							`${baseUrl}?instanceId=${this.instanceId}&nodeId=${this.nodeId}`
						)
						if (source === 0) {
							this.faceUrl = `https://learn.yilife.cn/?accessKeyId=${accessKeyId}&secretAccessKey=${secretAccessKey}&sessionToken=${sessionToken}&redirectUrl=${redirectUrl}&bytedToken=${res.data}&type=${type}&source=${source}&showResult=0`
						} else {
							this.faceUrl = `https://learn.yilife.cn/?accessKeyId=${accessKeyId}&secretAccessKey=${secretAccessKey}&sessionToken=${sessionToken}&redirectUrl=${redirectUrl}&type=${type}&source=${source}&showResult=0`
						}
						console.log(this.faceUrl)
					}
				})
		},
	},
}
</script>
<style lang="scss" scoped></style>
